package com.yuke.cloud.service.uac.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.core.annotation.OperationLogDto;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.service.api.uac.exceptions.UacBizException;
import com.yuke.cloud.service.uac.dao.AccLogMapper;
import com.yuke.cloud.service.uac.dto.AccLogMainDto;
import com.yuke.cloud.service.uac.entity.AccLog;
import com.yuke.cloud.service.uac.entity.AccPermission;
import com.yuke.cloud.service.uac.service.AccLogService;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.uac.service.AccPermissionService;
import com.yuke.cloud.service.uac.service.OpcRpcService;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * Created by wg on 2018/09/24.
 */
@Service
@Transactional
public class AccLogServiceImpl extends BaseService<AccLog> implements AccLogService {
    @Resource
    private AccLogMapper accLogMapper;

    @Resource
    private OpcRpcService opcRpcService;

    @Resource
    private AccPermissionService accPermissionService;

    @Override
    public int saveLog(AccLog accLog, LoginAuthDto loginAuthDto) {
        // 根据uri 查询url对应的权限
        AccPermission accPermission = accPermissionService.matchesByUrl(accLog.getRequestUrl());
        if (accPermission != null) {
            accLog.setPermissionId(accPermission.getPermissionId());
            accLog.setPermissionName(accPermission.getPermissionName());
        }

        accLog.setOperatorId(loginAuthDto.getUserId());
        accLog.setOperator(loginAuthDto.getUserName());
        if (accLog.getStartTime() == null) {
            accLog.setStartTime(new Date());
        }
        accLog.setLogId(this.generateId());
        return accLogMapper.insertSelective(accLog);
    }

    @Override
    public Integer saveOperationLog(OperationLogDto operationLogDto) {
        // 根据uri 查询url对应的权限
        AccPermission accPermission = accPermissionService.matchesByUrl(operationLogDto.getRequestUrl());
        if (accPermission != null) {
            operationLogDto.setPermissionName(accPermission.getPermissionName());
            operationLogDto.setPermissionId(accPermission.getPermissionId());
        }
        ModelMapper modelMapper = new ModelMapper();
        AccLog accLog = modelMapper.map(operationLogDto, AccLog.class);
        accLog.setLogId(generateId());
        // 获取操作位置
        String locationById = opcRpcService.getLocationById(operationLogDto.getIp());
        accLog.setLocation(locationById);
        return accLogMapper.insertSelective(accLog);
    }

    @Override
    public PageInfo queryLogListWithPage(AccLogMainDto accLogQueryDtoPage) {

        PageHelper.startPage(accLogQueryDtoPage.getPageNum(), accLogQueryDtoPage.getPageSize());
        List<AccLog> actionList = accLogMapper.queryLogListWithPage(accLogQueryDtoPage);
        return new PageInfo<>(actionList);
    }

    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public List<AccLog> selectUserLogListByUserId(Long userId) {
        if (PublicUtil.isEmpty(userId)) {
            throw new UacBizException(ErrorCodeEnum.UAC10011001);
        }

        Example example = new Example(AccLog.class);
        Example.Criteria criteria= example.createCriteria();
        criteria.andEqualTo("operatorId", userId);
        example.setOrderByClause("start_time desc");

        return accLogMapper.selectByExample(example);
    }

}
